package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author 小霞
 * @version 1.0
 * @description: 环信好友增删
 * @date 2021/7/7 19:34
 */
@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存好友关系
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public String saveUsers(Long userId, Long friendId) {
        //传来的数据都不能为空。
        if (!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return null;
        }

        //查看对方是否好友，如果不是就把他添加为好友
        Query quer = Query.query(Criteria.where("userId").is(userId).
                and("friendId").is(friendId));
        long count = this.mongoTemplate.count(quer, Users.class);
        //是好友关系
        if (count > 0) {
            return null;
        }
        //不是好友关系直接填充，加为好友
        Users users = new Users();
        users.setId(ObjectId.get());
        users.setUserId(userId);
        users.setFriendId(friendId);
        users.setDate(System.currentTimeMillis());

        //添加为好友关系
        this.mongoTemplate.save(users);

        //让对方也添加我为好友
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);

        this.mongoTemplate.save(users);
        return users.getId().toHexString();
    }

    /**
     * 删除好友数据
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
        if (!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return null;
        }

        //先查询是否好友
        Query quer = Query.query(Criteria.where("userId").is(userId).
                and("friendId").is(friendId));
        long count1 = this.mongoTemplate.count(quer, Users.class);
        if (count1 > 0) {
            //删除我与好友的关系数据
            long deletedCount = this.mongoTemplate.remove(quer, Users.class).getDeletedCount();

            Query query2 = Query.query(Criteria.where("userId").is(friendId)
                    .and("friendId").is(userId));
            //删除好友与我的关系数据
            long count2 = this.mongoTemplate.remove(query2, Users.class).getDeletedCount();

            return count1 > 0 && count2 > 0;
        }
        return null;
    }

    /**
     * 根据用户id查询全部Users列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryAllUsersList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.find(query, Users.class);
    }


    /**
     * 根据用户id查询Users列表(分页查询)
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {

        //设置分页，按照时间来排序
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);

        //拿到数据列表
        List<Users> usersList = this.mongoTemplate.find(query, Users.class);

        //添加数据
        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(usersList);

        return pageInfo;

    }
}
